//获取应用实例
var app = getApp();
var maxTime = 60000;
var interval = 5000;
var firstInterval = 10000;
// var maxTime = 10000;
// var interval = 2000;
// var firstInterval = 1000;
var timeInterval = null;//计算时间定时器
var queryInterval = null;//定时查询
var tiemOutInterval = null;//超时定时器
var orderParam = null;
Page({
    data: {

    },
    onLoad: function () {
        var that = this;
        orderParam = app.getStorageSync(app.globalData.cacheOrderPay);
        setTimeout(function () {//10秒后发起查询状态
            that.queryStatus();
        }, firstInterval);

        tiemOutInterval = setTimeout(function () {
            clearInterval(timeInterval);
            clearTimeout(queryInterval);
            orderParam['acceptStatus'] = "3";
            orderParam['acceptRemark'] = "";
            app.setStorageSync(app.globalData.cacheOrderPay, orderParam);
            app.redirectTo({url: '../../paysuccess/paysuccess'});
        }, maxTime);
    },
    queryStatus:function () {
        var that = this;
        var opt = {
            url: app.globalData.host + "/wx/cn/qas.ajax",
            data: {orderId: orderParam.orderId},
            showLoadFlag:"false",

            sucFn: function (result) {
            },
            completeFn: function (result) {
                // result.body.acceptRemark;

                if (result.body.acceptStatus == 0) {//0:受理中，5秒后发起查询状态
                    queryInterval = setTimeout(function () {
                        that.queryStatus()
                    }, interval);
                } else if (result.body.acceptStatus == 2||result.body.acceptStatus == 1) {//1:受理成功 2:受理失败
                    clearInterval(timeInterval);
                    clearTimeout(queryInterval);
                    clearTimeout(tiemOutInterval);
                    orderParam['acceptStatus'] = result.body.acceptStatus;
                    orderParam['acceptRemark'] = result.body.acceptRemark;
                    if(result.body.acceptStatus == 1){
                        var pageData = app.getStorageSync(app.globalData.cacheData);
                        pageData.cardNbr = null;
                        pageData.applicant = null;
                        pageData.isHandleSuccess = true;
                        app.setStorageSync(app.globalData.cacheData,pageData);
                    }
                    app.setStorageSync(app.globalData.cacheOrderPay, orderParam);
                    app.redirectTo({url: '../../paysuccess/paysuccess'});
                }else{
                    queryInterval = setTimeout(function () {
                        that.queryStatus()
                    }, interval);
                }
            }
        };
        app.ajax(opt);
    }
});
